package com.likelist.controller;

import java.io.IOException;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.Set;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.likelist.model.LikeListService;
import com.likelist.model.Like_listVO;
import com.orderdetail.model.Order_detailVO;

import front.membr.model.MemService;

/**
 * Servlet implementation class LikeServlet
 */
public class LikeServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	public void doGet(HttpServletRequest req, HttpServletResponse res)
			throws ServletException, IOException {
		doPost(req, res);
	}

	public void doPost(HttpServletRequest req, HttpServletResponse res)
			throws ServletException, IOException {
		req.setCharacterEncoding("UTF-8");

		String action = req.getParameter("action");
		Like_listVO cartgood = (Like_listVO) req.getAttribute("cartgood");
		
		if(cartgood instanceof Like_listVO) {
			HttpSession session = req.getSession();

			Set<Like_listVO> likelist = (Set<Like_listVO>) session.getAttribute("likelist");
			
			if (likelist == null) {
				MemService ms = new MemService();
				likelist = ms.getLikeListByMaccount(req.getParameter("maccount"));
				if(likelist.size() == 0) {
				
					LikeListService lls = new LikeListService();
					lls.addLikelist(cartgood.getMaccount() ,cartgood.getGoodsno());

					likelist.add(cartgood);
				}
				session.setAttribute("likelist", likelist);
			} else {

				
				boolean match = false;
				for(Like_listVO lvo : likelist) {
					if (lvo.getGoodsno().equals(cartgood.getGoodsno())) {
						match = true;
						break;
					}
				}
				if(match == false) {
					likelist.add(cartgood);
					
					LikeListService lls = new LikeListService();
					lls.addLikelist(req.getParameter("maccount"),
							Integer.parseInt(req.getParameter("goodsno")));
				}
				
				session.setAttribute("likelist", likelist);
			}
			
			String URL = req.getParameter("requestURL");

			RequestDispatcher dispatcher = req.getRequestDispatcher(URL);
			dispatcher.forward(req, res);

		}
		
		
		if ("check_list".equals(action)) {
			HttpSession session = req.getSession();

			Set<Like_listVO> likelist = (Set<Like_listVO>) session
					.getAttribute("likelist");

			
				MemService ms = new MemService();
				likelist = ms.getLikeListByMaccount(req
						.getParameter("maccount"));

				session.setAttribute("likelist", likelist);
			
			String url = req.getParameter("requestURL");

			RequestDispatcher dispatcher = req.getRequestDispatcher(url);
			dispatcher.forward(req, res);
		}

		if ("delete".equals(action)) {
			HttpSession session = req.getSession();

			Set<Like_listVO> likelist = (Set<Like_listVO>) session.getAttribute("likelist");

			Integer goodsno = Integer.parseInt(req.getParameter("goodsno"));
			
			LikeListService ls = new LikeListService();
			ls.deleteLikelist(req.getParameter("maccount"),
					Integer.parseInt(req.getParameter("goodsno")));
			
			for(Iterator ir = likelist.iterator(); ir.hasNext();) {
				Like_listVO lVO = (Like_listVO) ir.next();
				if(lVO.getGoodsno().equals(goodsno)) {
					ir.remove();
				}
			}
			session.setAttribute("likelist", likelist);

			String url = req.getParameter("requestURL");

			RequestDispatcher dispatcher = req.getRequestDispatcher(url);
			dispatcher.forward(req, res);
		}

		if ("add_list".equals(action)) {
			HttpSession session = req.getSession();

			Set<Like_listVO> likelist = (Set<Like_listVO>) session.getAttribute("likelist");
			
			if (likelist == null) {
				MemService ms = new MemService();
				likelist = ms.getLikeListByMaccount(req.getParameter("maccount"));
				if(likelist.size() == 0) {
					Like_listVO lVO = new Like_listVO();
					lVO.setMaccount(req.getParameter("maccount"));
					lVO.setGoodsno(Integer.parseInt(req.getParameter("goodsno")));
				
					LikeListService lls = new LikeListService();
					lls.addLikelist(req.getParameter("maccount"),
							Integer.parseInt(req.getParameter("goodsno")));

					likelist.add(lVO);
				}
				session.setAttribute("likelist", likelist);
			} else {

				Like_listVO newLVO = new Like_listVO();
				newLVO .setMaccount(req.getParameter("maccount"));
				newLVO .setGoodsno(Integer.parseInt(req.getParameter("goodsno")));
				
				boolean match = false;
				for(Like_listVO lvo : likelist) {
					if (lvo.getGoodsno().equals(newLVO.getGoodsno())) {
						match = true;
						break;
					}
				}
				if(match == false) {
					likelist.add(newLVO);
					
					LikeListService lls = new LikeListService();
					lls.addLikelist(req.getParameter("maccount"),
							Integer.parseInt(req.getParameter("goodsno")));
				}
				
				session.setAttribute("likelist", likelist);
			}
			
			String URL = req.getParameter("requestURL");

			RequestDispatcher dispatcher = req.getRequestDispatcher(URL);
			dispatcher.forward(req, res);

		}
		
		if("transfer_to_cart".equals(action)) {
			HttpSession session = req.getSession();

			Set<Like_listVO> likelist = (Set<Like_listVO>) session.getAttribute("likelist");
			
		
			String maccout = req.getParameter("maccount");
			Integer goodsno = Integer.parseInt(req.getParameter("goodsno"));
			String goods_type = req.getParameter("goodstype");
			String price = req.getParameter("price");
			String qty = req.getParameter("qty");
			
			LikeListService ls = new LikeListService();
			ls.deleteLikelist(maccout, goodsno);
			
			for(Iterator ir = likelist.iterator(); ir.hasNext();) {
				Like_listVO lVO = (Like_listVO) ir.next();
				if(lVO.getGoodsno().equals(goodsno)) {
					ir.remove();
				}
			}
			
			session.setAttribute("likelist" ,likelist);
			
			Order_detailVO orVO = new Order_detailVO();
			
			orVO.setGoodsno(goodsno);
			orVO.setGood_type(Integer.parseInt(goods_type));
			orVO.setPrice(Integer.parseInt(price));
			orVO.setOrder_qt(Integer.parseInt(qty));
			
			req.setAttribute("likelistgood" , orVO);

			RequestDispatcher dispatcher = req.getRequestDispatcher("/cart/CartServlet.do");
			dispatcher.forward(req ,res);
		}
	}
}
